package com.yzy.soft.system.freemarker;

//import com.evada.inno.pm.code.generate.model.ColumnClass;

import freemarker.template.Template;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * ����������������
 * Created by Ay on 2017/5/1.
 */
public class FormCodeGenerateUtils {

    private final String AUTHOR = "Yinzhiyuan";
//    private final String CURRENT_DATE = "2017/05/03";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);  
    Calendar calendar=Calendar.getInstance();
    private final String CURRENT_DATE = format.format(calendar.getTime());
    private final String tableName = "tm_project_quality_problem";
    private final String packageName = "com.sinosoft.liscw.system";
    private final String tableAnnotation = "表单";
    private final String controllerFileName = "Form";
    private final String class_remark = "";
    private final String class_requestMapping = "form";
//    private final String URL = "jdbc:postgresql://192.168.3.160:10655/cibpm";
//    private final String USER = "postgres";
//    private final String PASSWORD = "888888";
//    private final String DRIVER = "org.postgresql.Driver";
    private final String path="//com//sinosoft//liscw//system//controller//";
    private final String diskPath = "D:\\BaiduYunDownload\\liscw-parent\\liscw-admin\\src\\main\\java"+path;
//    private final String changeTableName = replaceUnderLineAndUpperCase(tableName);

//    public Connection getConnection() throws Exception{
//        Class.forName(DRIVER);
//        Connection connection= DriverManager.getConnection(URL, USER, PASSWORD);
//        return connection;
//    }

    public static void main(String[] args) throws Exception{
        FormCodeGenerateUtils testCodeGenerateUtils = new FormCodeGenerateUtils();
        testCodeGenerateUtils.generate();
    }

    public void generate() throws Exception{
        try {
//            Connection connection = getConnection();
//            DatabaseMetaData databaseMetaData = connection.getMetaData();
//            ResultSet resultSet = databaseMetaData.getColumns(null,"%", tableName,"%");
            //����Mapper�ļ�
//            generateMapperFile(resultSet);
//            //����Dao�ļ�
//            generateDaoFile(resultSet);
//            //����Repository�ļ�
//            generateRepositoryFile(resultSet);
//            //���ɷ����ӿ��ļ�
//            generateServiceInterfaceFile(resultSet);
//            //���ɷ���ʵ�ֲ��ļ�
//            generateServiceImplFile(resultSet);
            //����Controller���ļ�
            generateControllerFile();
//            //����DTO�ļ�
//            generateDTOFile(resultSet);
//            //����Model�ļ�
//            generateModelFile(resultSet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally{

        }
    }
    
    
    private void generateControllerFile() throws Exception{
        final String suffix = "Controller.java";
        final String path = diskPath + controllerFileName + suffix;
        final String templateName = "Controller.ftl";
        File mapperFile = new File(path);
        Map<String,Object> dataMap = new HashMap<String,Object>();
        generateFileByTemplate(templateName,mapperFile,dataMap);
    }
    
    

//    private void generateModelFile(ResultSet resultSet) throws Exception{
//
//        final String suffix = ".java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "Model.ftl";
//        File mapperFile = new File(path);
//        List<ColumnClass> columnClassList = new ArrayList<ColumnClass>();
//        ColumnClass columnClass = null;
//        while(resultSet.next()){
//            //id�ֶ��Թ�
//            if(resultSet.getString("COLUMN_NAME").equals("id")) continue;
//            columnClass = new ColumnClass();
//            //��ȡ�ֶ�����
//            columnClass.setColumnName(resultSet.getString("COLUMN_NAME"));
//            //��ȡ�ֶ�����
//            columnClass.setColumnType(resultSet.getString("TYPE_NAME"));
//            //ת���ֶ����ƣ��� sys_name ��� SysName
//            columnClass.setChangeColumnName(replaceUnderLineAndUpperCase(resultSet.getString("COLUMN_NAME")));
//            //�ֶ������ݿ��ע��
//            columnClass.setColumnComment(resultSet.getString("REMARKS"));
//            columnClassList.add(columnClass);
//        }
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        dataMap.put("model_column",columnClassList);
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//
//    }
//
//    private void generateDTOFile(ResultSet resultSet) throws Exception{
//        final String suffix = "DTO.java";
//        final String path = "D://" + changeTableName + suffix;
//        final String templateName = "DTO.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    
//
//    private void generateServiceImplFile(ResultSet resultSet) throws Exception{
//        final String suffix = "ServiceImpl.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "ServiceImpl.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateServiceInterfaceFile(ResultSet resultSet) throws Exception{
//        final String prefix = "I";
//        final String suffix = "Service.java";
//        final String path = diskPath + prefix + changeTableName + suffix;
//        final String templateName = "ServiceInterface.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateRepositoryFile(ResultSet resultSet) throws Exception{
//        final String suffix = "Repository.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "Repository.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateDaoFile(ResultSet resultSet) throws Exception{
//        final String suffix = "DAO.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "DAO.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//
//    }
//
//    private void generateMapperFile(ResultSet resultSet) throws Exception{
//        final String suffix = "Mapper.xml";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "Mapper.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//
//    }
//
    private void generateFileByTemplate(final String templateName,File file,Map<String,Object> dataMap) throws Exception{
        if(file.exists()){
            file.deleteOnExit();
        }
        Template template = FreeMarkerTemplateUtils.getTemplate(templateName);
        FileOutputStream fos = new FileOutputStream(file);
        dataMap.put("table_name_small",tableName);
        dataMap.put("table_name",controllerFileName);
        dataMap.put("author",AUTHOR);
        dataMap.put("date",CURRENT_DATE);
        dataMap.put("package_name",packageName);
        dataMap.put("table_annotation",tableAnnotation);
        dataMap.put("class_requestMapping",class_requestMapping);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);
    }



    public static String generateStringByTemplate(String templateName,Map<String,Object> dataMap) throws Exception{
//        final String suffix = ".java";
//        final String suffix = "Controller.java";
//        final String path = diskPath + controllerFileName + suffix;
        templateName = "form.ftl";
        Template template = FreeMarkerTemplateUtils.getTemplate(templateName);
        Writer out = new StringWriter(2048);
        template.process(dataMap, out);
        return out.toString();
    }
//
//    public String replaceUnderLineAndUpperCase(String str){
//        StringBuffer sb = new StringBuffer();
//        sb.append(str);
//        int count = sb.indexOf("_");
//        while(count!=0){
//            int num = sb.indexOf("_",count);
//            count = num + 1;
//            if(num != -1){
//                char ss = sb.charAt(count);
//                char ia = (char) (ss - 32);
//                sb.replace(count , count + 1,ia + "");
//            }
//        }
//        String result = sb.toString().replaceAll("_","");
//        return StringUtils.capitalize(result);
//    }

}
 

